using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace url2jpeg
{
    public partial class navigation : LocalizedUserControl
    {

        public delegate void GoToCaptureHandler(url2jpegCtn.navigator navigator);
        public event GoToCaptureHandler GoToCaptureEvent;
        private bool m_bFormLoaded = false, m_bPageNavigated = false;
        private List<LinkLabel> m_newPageLabels;

        public navigation()
        {
            InitializeComponent();
            cboInitialSize.SelectedIndex = 0;
            m_newPageLabels = new List<LinkLabel>();
            lblExternalLinks.Visible = false;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            navigator1.Enabled = false;
            navigator1.ScrollBarsEnabled(!btnScrollBars.Checked);
            navigator1.AutoResize(btnAutoResize.Checked, GetTextInt(txtWidth), GetTextInt(txtHeight));
            navigator1.Enabled = true;
            navigator1.Navigate(txtURL.Text);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            navigator1.Stop();
        }

        private void btnScrollBars_CheckedChanged(object sender, EventArgs e)
        {
            navigator1.ScrollBarsEnabled(!btnScrollBars.Checked);
        }

        static public int GetTextInt(TextBox tb)
        {
            int result = 0;
            try
            {
                result = Convert.ToInt32(tb.Text);
            }
            catch
            { }
            return result;
        }

        private void btnAutoResize_CheckedChanged(object sender, EventArgs e)
        {
            navigator1.AutoResize(btnAutoResize.Checked, GetTextInt(txtWidth), GetTextInt(txtHeight));
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            timerTextChanged.Enabled = false;
            timerTextChanged.Enabled = true;
        }

        private void timerTextChanged_Tick(object sender, EventArgs e)
        {
            timerTextChanged.Enabled = false;
            navigator1.AutoResize(btnAutoResize.Checked, GetTextInt(txtWidth), GetTextInt(txtHeight));
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            navigator1.Stop();
            if (GoToCaptureEvent != null && m_bPageNavigated)
                GoToCaptureEvent(navigator1);
        }

        private void navigator1_PageLoadedEvent(string url)
        {
            m_bPageNavigated = true;
            if (url != "http://www.url2jpeg.com/stats.html")
                txtURL.Text = url;
        }

        private void navigation_SizeChanged(object sender, EventArgs e)
        {
            if (cboInitialSize.SelectedIndex == 0 && m_bFormLoaded)
                navigator1.Size = new Size(ClientRectangle.Width, ClientRectangle.Height - navigator1.Location.Y);
        }

        private void cboInitialSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboInitialSize.SelectedIndex == 0)
                navigation_SizeChanged(null, null);
            else if (cboInitialSize.SelectedIndex > 0)
            {
                string initialSize = cboInitialSize.SelectedItem.ToString();
                string[] parts = initialSize.Split('x');
                if (parts.Length == 2)
                    navigator1.Size = new Size(Convert.ToInt32(parts[0]), Convert.ToInt32(parts[1]));
            }
        }

        private void navigation_Load(object sender, EventArgs e)
        {
            navigator1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left;
            navigator1.Navigate("http://www.url2jpeg.com/stats.html");
            m_bFormLoaded = true;
        }

        private void navigator1_NewWindowEvent(string url)
        {
            LinkLabel ll = new LinkLabel();
            ll.Text = url;
            ll.AutoSize = true;
            ll.Click += new EventHandler(ll_Click);
            m_newPageLabels.Add(ll);
            flowLayoutPanel1.Controls.Add(ll);
            lblExternalLinks.Visible = true;
        }

        void ll_Click(object sender, EventArgs e)
        {
            LinkLabel ll = sender as LinkLabel;
            navigator1.Navigate(ll.Text);
        }

        private void navigator1_BeforeNavigateEvent(string url)
        {
            foreach (LinkLabel ll in m_newPageLabels)
                flowLayoutPanel1.Controls.Remove(ll);
            m_newPageLabels.Clear();
            lblExternalLinks.Visible = false;
        }

        // Fill scriptData object with current page info
        public bool FillForScript(scriptData data)
        {
            bool bResult = false;
            if (navigator1.browser.Document != null || navigator1.browser.Url != null)
            {
                if (navigator1.browser.Document != null)
                    data.name = navigator1.browser.Document.Title;
                else
                    data.name = navigator1.browser.Url.ToString();
                data.url = navigator1.browser.Url.ToString();
                data.initialSize = navigator1.Size;
                data.bAutoResize = btnAutoResize.Checked;
                data.bRemoveScrollBar = btnScrollBars.Checked;
                data.sizeOffset = new Size(GetTextInt(txtWidth), GetTextInt(txtHeight));
                data.timeOut = 10;
                bResult = true;
            }
            return bResult;
        }

        private void txtURL_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnGo_Click(null, null);
            if (e.KeyCode == Keys.F10)
                btnCapture_Click(null, null);
        }
    }
}
